import groovy.sql.Sql
import java.text.*


class ExtractionController extends Pacx {

    def extractiondir, repositorydir, extractorhome, extractionskip 
    
    ExtractionController() {
        super()
        log = new File(config.doc4.extraction.logfile)
        extractiondir = config.doc4.extraction.outputdir
        repositorydir = config.doc4.extraction.mirror
        extractorhome = config.doc4.extraction.scriptdir
        extractionskip = config.doc4.extraction.skipifextracted


    }

    public static void main(String[] args) {
        def exx = new ExtractionController()
        exx.extractPackages()  
        exx.sql.close()  
    }
    

    def void extractPackages() {
        //don't treat two times the same package in the same run or the result of the last try will override the previous ones
        //i.e. "skipped" will override any potential previous error during the extraction itself
        def done = new TreeSet()
        
        def counter  = 0
        def total = 0
        def queries = config.doc4.extraction.queries
        sql.eachRow(queries.count){
            row -> 
                log("${row.architecture} ${row.branch} ${row.section}: ${row.ct}")
                total += row.ct
        }
    
        log("Total: $total")
        
        sql.eachRow(queries.queue) {
            row ->
                def pkg = row.pkg;
                def distrib = row.distribution;
                def archfolder = row.architecture_folder;
                def arch = row.architecture;
                def branch = row.branch;
                def section = row.section;
                if (!done.contains(pkg)) {
                    done.add(pkg)
                    def output_dir = "$extractiondir/"+stringToInteger(pkg)+"/"
                    
                    def targetFolder = new File(output_dir+pkg)
                    if (targetFolder.exists()) {
                        if (extractionskip) {
                            log("folder $targetFolder exists already -> skipping")
                            setPackageState(pkg, "end_date", "skipped", "")
                        } else {
                            def result = targetFolder.deleteDir()
                            log("deletion of $targetFolder: $result")
                            if (!result) {
                                setPackageState(pkg, "end_date","finished", "the already existing extraction folder could not be deleted")
                                
                            }
                        }
                    }
                    //now it may have disappeared
                    if (!targetFolder.exists()) {
                        
                        //select floor(length(package)/ASCII(package)*1000)
                        def media = arch.equals("SRPMS") ? "" : "media/"
                        def pkgdir = "$repositorydir/$distrib/$archfolder/$media/$branch/$section/"
                        def command = "python $extractorhome/RpmExtractor.py -p $pkg -a $archfolder -b $branch -s $section -i $pkgdir -o $output_dir -t $extractorhome -l "+log.getAbsolutePath()
                        log(command)
                        setPackageState(pkg, "start_date", "started", "")
                        def proc = command.execute()
                        proc.consumeProcessOutput()
                        proc.waitFor()
                        def exitValue = proc.exitValue()
                        def stderror = proc.err.text
                        def stdout = proc.in.text
                        log("return code: $exitValue")
                        if (exitValue ==0) {
                                setPackageState(pkg, "end_date","finished", "")
                                //delete the package file and add its relative path to the list of rsync excludes
                                def package_path = "$pkgdir/$pkg"
                                //command = "rm $package_path"
                                //proc = command.execute()
                                //proc.waitFor()
                                //def deleted = proc.exitValue()
                               
            
                        } else {
                            log("stdout: $stdout stderr: $stderror")
                            setPackageState(pkg, "end_date", "failed", stdout+"\n"+stderror)
                        }
                        
                        counter++
                      
                    }   
                } 
        }
        //    if self.metadata_only == 'true':
        //                command = command+' --metadata-only'

}


def void setPackageState(String pkg, String dateField, String state, String error) {
    def date = sdf.format(new Date());      
    error = error.replaceAll("'","\\\\'")
    def query = "update PACKAGE_QUEUE SET state=?, $dateField=?, error=? where package=? and action='extraction'"
    sql.execute(query, [state, date, error, pkg])
}


}